﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;


namespace SGCP_UI
{
    public partial class wfr_proveedorCotizacion : System.Web.UI.Page, ICallbackEventHandler
    {
        localhost.Service obj;
       
        
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new localhost.Service();
            
            String cbReference =
              Page.ClientScript.GetCallbackEventReference(this,
              "arg", "script_AdministrarCotizacion", "context");

            String callbackScript;
            callbackScript = "function llamarServidor(arg, context)" +
                "{" + cbReference + ";}";
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                "llamarServidor", callbackScript, true);
        }
        private string _callbackArgument;

        public string GetCallbackResult()
        {
            return _callbackArgument;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            Label lbl = new Label();
            try
            {
                string[] vector;
                vector = new string[3];
                vector = separarArgumentos(eventArgument);
                DataSet ds = new DataSet();

                if (vector[0] == "BuscarPorFecha")
                {
                    if (ds  != null)
                    {
                        gvFecha.DataSource = ds;
                        gvFecha.DataBind();

                        lbl.Text = "ConsultaPorFecha";
                        StringWriter sr = new StringWriter();
                        HtmlTextWriter htm = new HtmlTextWriter(sr);
                        lbl.RenderControl(htm);
                        gvFecha.RenderControl(htm);
                        htm.Flush();
                        _callbackArgument = sr.ToString();
                    }
                    else
                    {
                        lbl.Text = "No se han encontrado Resultados";
                        StringWriter sr = new StringWriter();
                        HtmlTextWriter htm = new HtmlTextWriter(sr);
                        lbl.RenderControl(htm);
                        htm.Flush();
                        _callbackArgument = sr.ToString();
                    }
                }
                if (vector[0] == "BuscarPorCodigo")
                {
                    if ((ds = AdministrarCotizacionxCodigo(vector)) != null)
                    {
                        gvCodigo.DataSource = ds;
                        gvCodigo.DataBind();

                        lbl.Text = "ConsultaPorCodigo";
                        StringWriter sr = new StringWriter();
                        HtmlTextWriter htm = new HtmlTextWriter(sr);
                        lbl.RenderControl(htm);
                        gvCodigo.RenderControl(htm);
                        htm.Flush();
                        _callbackArgument = sr.ToString();
                    }
                    else
                    {
                        lbl.Text = "No se han encontrado Resultados";
                        StringWriter sr = new StringWriter();
                        HtmlTextWriter htm = new HtmlTextWriter(sr);
                        lbl.RenderControl(htm);
                        htm.Flush();
                        _callbackArgument = sr.ToString();
                    }
                }
                if (vector[0] == "ActualizarPago")
                {
                    if (ds != null)
                    {
                        gvCodigo.DataSource = ds;
                        gvCodigo.DataBind();

                        lbl.Text = "ConsultaPorCodigo";
                        StringWriter sr = new StringWriter();
                        HtmlTextWriter htm = new HtmlTextWriter(sr);
                        lbl.RenderControl(htm);
                        gvCodigo.RenderControl(htm);
                        htm.Flush();
                        _callbackArgument = sr.ToString();
                    }
                    else
                    {
                        lbl.Text = "No se han encontrado Resultados";
                        StringWriter sr = new StringWriter();
                        HtmlTextWriter htm = new HtmlTextWriter(sr);
                        lbl.RenderControl(htm);
                        htm.Flush();
                        _callbackArgument = sr.ToString();
                    }
                }

            }
            catch (Exception ex)
            {
                lbl.Text = ex.ToString();
                StringWriter sr = new StringWriter();
                HtmlTextWriter htm = new HtmlTextWriter(sr);
                lbl.RenderControl(htm);
                htm.Flush();
                _callbackArgument = sr.ToString();
            }
        }
        public static string[] separarArgumentos(string parametro)
        {
            string todoJunto = parametro.ToString();
            string[] vector;
            vector = new string[3];
            int contador = 0;
            for (int i = 0; i < todoJunto.Length; i++)
            {
                if (todoJunto[i].ToString() != "$")
                {
                    vector[contador] += todoJunto[i].ToString();
                }
                else
                {
                    contador++;
                }

            }
            return vector;
        }


          public DataSet ObtenerDetalleCotizacion(string[] vector1)
        {
            localhost.dtoCotizacion odtocotizacion = new localhost.dtoCotizacion();
            odtocotizacion.id_cotizacion = Convert.ToInt32(vector1[1]);
            return obj.ObtenerDetalleCotizacion(odtocotizacion);
        }


      

        public DataSet AdministrarCotizacionxCodigo(String[] Vec)
        {
            localhost.dtoCotizacion dto_cotizacion = new localhost.dtoCotizacion();
            dto_cotizacion.id_cotizacion = Convert.ToInt32(Vec[1]);
            

            return obj.AdministrarCotizacionCodigo(dto_cotizacion);
        }

        protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Click":
                    {
                        string FirstColumnValue = e.CommandArgument.ToString();
                        HiddenField1.Value = FirstColumnValue;
                        break;
                    }
                default:
                    break;
            }
        }

       

       

    }
}